package cn.nfwx.tv.adapter;

import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.lcodecore.tkrefreshlayout.utils.DensityUtil;

import java.util.ArrayList;
import java.util.List;

import cn.nfwx.tv.R;
import cn.nfwx.tv.conts.Constants;
import cn.nfwx.tv.model.QpopChannel;
import cn.nfwx.tv.model.TvChannel;
import cn.nfwx.tv.util.GlideUtil;

public class ChannelAdapter2 extends BaseQuickAdapter<Object, BaseViewHolder> {
    private boolean _init;
    private int selectedPosition = -5;

    public ChannelAdapter2() {
        super(R.layout.activity_channel_item);
    }

    public void setSelected(int position) {
        this.selectedPosition = position;
    }

    @Override
    protected void convert(BaseViewHolder helper, Object object) {
        helper.getView(R.id.iv_channel_placeholder).setVisibility(View.GONE);
        if (_init) {
            helper.getView(R.id.iv_channel_placeholder).setVisibility(View.VISIBLE);
            return;
        }
        if (object instanceof QpopChannel.DataBean.Channel) {
            QpopChannel.DataBean.Channel channel = (QpopChannel.DataBean.Channel) object;
            GlideUtil.loadRoundImage(mContext, Constants.Qpop_url + channel.getImgUrl(), (ImageView) helper.getView(R.id.channel_image), GlideUtil.ScaleType.DEFAULT);
            ((TextView) helper.getView(R.id.tv_channel_name)).setText(channel.getTitle());

            if (selectedPosition == helper.getLayoutPosition()) {
                if (TextUtils.isEmpty(channel.getCurrent())) {
                    ((TextView) helper.getView(R.id.tv_program_name)).setText("正在直播");
                } else {
                    ((TextView) helper.getView(R.id.tv_program_name)).setText(channel.getCurrent());
                }
                helper.getView(R.id.channel_image).setSelected(true);
                helper.getView(R.id.channel_image).getLayoutParams().width = DensityUtil.dp2px(helper.itemView.getContext(), 42);
                helper.getView(R.id.channel_image).getLayoutParams().height = DensityUtil.dp2px(helper.itemView.getContext(), 42);
                //holder.tv_btn.setSelected(true);
                helper.getView(R.id.tv_program_name).setVisibility(View.VISIBLE);
                helper.getView(R.id.tv_program_name).setSelected(true);
                ((TextView) helper.getView(R.id.tv_program_name)).setTextColor(mContext.getResources().getColor(R.color.program_text));
            } else {
                helper.getView(R.id.channel_image).setSelected(false);
                //holder.tv_btn.setSelected(false);
                helper.getView(R.id.tv_program_name).setVisibility(View.GONE);
                helper.getView(R.id.tv_program_name).setSelected(false);
                // holder.tv_btn.setBackgroundResource(R.drawable.shape_corner_up);
            }
//            holder.itemView.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//                    int position = holder.getLayoutPosition();
//                    onItemClickListener.onOnItemClick(view, position);
//                    selectedPosition = position;
//                    notifyItemChanged(selectedPosition);
//                }
//            });
        } else if (object instanceof TvChannel) {
            TvChannel tvChannel = (TvChannel) object;
            GlideUtil.loadRoundImage(mContext, tvChannel.getLogo(), (ImageView) helper.getView(R.id.channel_image), GlideUtil.ScaleType.DEFAULT);
            ((TextView) helper.getView(R.id.tv_channel_name)).setText(tvChannel.getName());

            if (selectedPosition == helper.getLayoutPosition()) {
                if (tvChannel.getPlayingProgram() != null) {
                    if (TextUtils.isEmpty(tvChannel.getPlayingProgram().getProgram_name())) {
                        ((TextView) helper.getView(R.id.tv_program_name)).setText("正在直播");
                    } else {
                        ((TextView) helper.getView(R.id.tv_program_name)).setText(tvChannel.getPlayingProgram().getProgram_name());
                    }
                } else {
                    if (TextUtils.isEmpty(tvChannel.getProgram_name())) {
                        ((TextView) helper.getView(R.id.tv_program_name)).setText("正在直播");
                    } else {
                        ((TextView) helper.getView(R.id.tv_program_name)).setText(tvChannel.getProgram_name());
                    }
                }
                helper.getView(R.id.channel_image).setSelected(true);
                helper.getView(R.id.channel_image).getLayoutParams().width = DensityUtil.dp2px(helper.itemView.getContext(), 42);
                helper.getView(R.id.channel_image).getLayoutParams().height = DensityUtil.dp2px(helper.itemView.getContext(), 42);
                //holder.tv_btn.setSelected(true);
                helper.getView(R.id.tv_program_name).setVisibility(View.VISIBLE);
                helper.getView(R.id.tv_program_name).setSelected(true);
                ((TextView) helper.getView(R.id.tv_program_name)).setTextColor(mContext.getResources().getColor(R.color.program_text));
            } else {
                helper.getView(R.id.channel_image).setSelected(false);
                //holder.tv_btn.setSelected(false);
                helper.getView(R.id.tv_program_name).setVisibility(View.GONE);
                helper.getView(R.id.tv_program_name).setSelected(false);
                // holder.tv_btn.setBackgroundResource(R.drawable.shape_corner_up);
            }
        }
    }

    @Override
    public void setNewData(@Nullable List<Object> data) {
        _init = false;
        super.setNewData(data);
    }

    /**
     * 初始化信息，队长说要
     */
    public void initData() {
        List<Object> tvChannelList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Object livemode = new Object();
            tvChannelList.add(livemode);
        }
        setNewData(tvChannelList);
        _init = true;
    }

    /**
     * 更新正在播放的节目
     *
     * @param position
     * @param channel
     */
    public void notifyItemChanged(int position, TvChannel channel) {
        if (getData().isEmpty() || channel == null) {
            notifyItemChanged(position);
        } else {
            for (Object o : getData()) {
                if (o instanceof TvChannel) {
                    TvChannel tvChannel = (TvChannel) o;
                    if (tvChannel.getGid().equals(channel.getGid())) {
                        tvChannel.setPlayingProgram(channel.getPlayingProgram());
                        break;
                    }
                }
            }
            notifyItemChanged(position);
        }
    }
}